package com.lhf.dajiuye.mapper.dict;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lhf.dajiuye.bean.dict.Dict;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 字典表 Mapper 接口
 * </p>
 *
 * @author xiayuhao
 * @since 2023-10-17
 */
@Mapper
public interface DictMapper extends BaseMapper<Dict> {
    /**
     * 根据type获取字典Dict list
     * @param type
     * @return
     */
    @Select("<script>select b.* from dict b where b.is_stop='0' and b.parent_id=\n" +
            "(SELECT a.id FROM `dict` a where a.parent_id='0' and a.type=#{type} and a.is_stop='0')\n" +
            "order by order_num</script>")
    List<Dict> getDictListByType(@Param("type") String type);

    /**
     * 根据parentId获取字典Dict list
     * @param parentId
     * @return
     */
    @Select("<script>select b.* from dict b where b.is_stop='0' and b.parent_id=#{parentId} order by order_num</script>")
    List<Dict> getDictListByPid(@Param("parentId") String parentId);

    /**
     * 根据type获取字典Dict listTree
     * @param type
     * @return
     */
    @Select("<script>select t3.id,t3.type,t3.name,t3.parent_id,t3.is_leaf,t3.order_num,t3.remark,t3.is_stop,t3.level\n" +
            " from (\n" +
            "\t select t1.id,t1.type,t1.name,t1.parent_id,t1.is_leaf,t1.order_num,t1.remark,t1.is_stop,t1.level,\n" +
            "\t if(find_in_set(parent_id, @pids) > 0, @pids := concat(@pids, ',', id), 0) as ischild\n" +
            "\t from \n" +
            "\t\t (select t.id,t.type,t.name,t.parent_id,t.is_leaf,t.order_num,t.remark,t.is_stop,t.level\n" +
            "\t\t\t from dict t where t.is_stop = '0' order by t.parent_id,t.id) t1,\n" +
            "\t\t (select @pids := (select id from dict where type=#{type} and is_stop='0')) t2\n" +
            " ) t3 where ischild != 0 order by t3.level,t3.order_num</script>")
    List<Dict> getDictListTreeByType(@Param("type") String type);

    /**
     * 根据id获取字典Dict
     * @param id
     * @return
     */
    @Select("<script>select t.id,t.type,t.name,t.parent_id,t.is_leaf,t.order_num,t.remark,t.is_stop,t.level from dict t \n" +
            "where t.is_stop = '0' and t.id=#{id}</script>")
    Dict getDictById(@Param("id") String id);
}
